package com.yanqu.road.server.manager.activity.zhounianturntable;


import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.zhounianturntable.ZhouNianTurntableRecord;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.bussiness.activity.ZhouNianTurntableBusiness;
import com.yanqu.road.pb.activity.ZhouNianTurntableProto;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameProtocol;

import java.util.ArrayList;
import java.util.List;

public class ZhouNianTurntableActivity {

    private ActivityInfo activityInfo;

    /**
     * 记录最大数量
     */
    public static final int MAX_ZHOUNIAN_TURNTABLE_RECORD_COUNT = 100;

    private List<ZhouNianTurntableRecord> noticeList;



    public ZhouNianTurntableActivity(ActivityInfo activityInfo) {
        this.activityInfo = activityInfo;
        noticeList = ZhouNianTurntableBusiness.getZhouNianTurntableRecordList(activityInfo.getActivityId(), MAX_ZHOUNIAN_TURNTABLE_RECORD_COUNT);
    }

    public void save() {
        int size = noticeList.size();
        for (int i = 0; i < size; i++) {
            ZhouNianTurntableRecord record = noticeList.get(i);
            if (record.isInsertOption()) {
                ZhouNianTurntableBusiness.addZhouNianTurntableRecord(record);
            }
        }
    }

    /**
     * 添加公告
     */
    public void uploadRecord(long userId, String reward, UserBaseInfo userBaseInfo) {
        ZhouNianTurntableRecord record = new ZhouNianTurntableRecord();
        record.setActivityId(activityInfo.getActivityId());
        record.setUserId(userId);
        record.setUserBaseInfo(userBaseInfo);
        record.setReward(reward);
        record.setTime(System.currentTimeMillis() / 1000);
        record.setInsertOption();

        synchronized (noticeList) {
            noticeList.add(record);
        }

        // 同步到区服
        ZhouNianTurntableProto.SyncTurntableRecordRespMsg.Builder builder = ZhouNianTurntableProto.SyncTurntableRecordRespMsg.newBuilder();
        builder.setNotice(CrossZhouNianTurntableMgr.parseRecordMsg(record));
        YanQuMessage YanQuMessage = YanQuMessageUtils.buildMessage(GameProtocol.S_ZHOUNIAN_TURNTABLE_SYNC_RECORD, builder);
        for (Long serverId : activityInfo.getServerIdList()) {
            MessageHelper.sendPacket(serverId, 0, YanQuMessage);
        }
    }

    /**
     * 获取公告
     */
    public List<ZhouNianTurntableRecord> getRecordData() {
        List<ZhouNianTurntableRecord> tmpList = new ArrayList<>(noticeList);
        if (tmpList.size() > MAX_ZHOUNIAN_TURNTABLE_RECORD_COUNT) {
            List<ZhouNianTurntableRecord> result = new ArrayList<>();
            int size = tmpList.size();
            for (int i = size - 1; i >= size - MAX_ZHOUNIAN_TURNTABLE_RECORD_COUNT; i--) {
                result.add(tmpList.get(i));
            }
            return tmpList.subList(tmpList.size() - MAX_ZHOUNIAN_TURNTABLE_RECORD_COUNT, tmpList.size());
        } else {
            return tmpList;
        }
    }

}
